package cn.afterturn.easypoi.test.excel.export;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.ss.usermodel.Workbook;

import cn.afterturn.easypoi.test.entity.MsgClient;
import cn.afterturn.easypoi.test.entity.MsgClientGroup;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelEntity;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import org.junit.Test;

/**
 * 当行大数据量测试(bean 包含 bean )
 */
public class ExcelExportMsgClient {

	@Test
	public void test() throws Exception {

		Field[] fields = MsgClient.class.getFields();
		for (int i = 0; i < fields.length; i++) {
			Excel excel = fields[i].getAnnotation(Excel.class);
			System.out.println(excel);
		}
		// start:**********************************************************************************/
		// 使用@Excel(name = "电话号码") 导出列；MsgClient这个bean下MsgClientGroup 使用注解
		// @ExcelEntity 引入该bean 再用@Excel(name = "") 将该bean下的列导出
		// end:**********************************************************************************/
		List<MsgClient> list = new ArrayList<MsgClient>();
		for (int i = 0; i < 100; i++) {
			MsgClient client = new MsgClient();
			client.setBirthday(new Date());
			client.setClientName("小明" + i);
			client.setClientPhone("18797" + i);
			client.setCreateBy("JueYue");
			client.setId("1" + i);
			client.setRemark("测试" + i);
			MsgClientGroup group = new MsgClientGroup();
			group.setGroupName("分组" + i);
			group.setCreateBy("创建人" + i);
			client.setGroup(group);
			list.add(client);
		}
		Date start = new Date();
		ExportParams params = new ExportParams("2412312", "测试", ExcelType.XSSF);
		params.setFreezeCol(2);
		Workbook workbook = ExcelExportUtil.exportExcel(params, MsgClient.class, list);
		System.out.println(new Date().getTime() - start.getTime());
		File savefile = new File("c:/excelllllllll/");
		if (!savefile.exists()) {
			savefile.mkdirs();
		}
		FileOutputStream fos = new FileOutputStream("c:/excelllllllll/ExcelExportMsgClient.xlsx");
		workbook.write(fos);
		fos.close();
	}

}
